using System;
using System.Collections.Generic;

namespace roster.Domain
{
	public sealed class RaidFileRegistrar
	{
		private static RaidFileRegistrar instance = new RaidFileRegistrar();
		
		public static RaidFileRegistrar Instance {
			get {
				return instance;
			}
		}
		
		public void RegisterRaidFileType(string fileExtension, IRaidFileReader fileReader)
		{
			if (m_registeredReaders.ContainsKey(fileExtension))
			{
				string msg = string.Format("Raid file reader ({0}) already registered!", fileExtension);
				throw new ArgumentException(msg);
			}
			
			m_registeredReaders.Add(fileExtension, fileReader);
		}
		
		public IRaidFileReader GetReader(string fileName)
		{
			string fileExtension = fileName.Substring(fileName.LastIndexOf('.') + 1);
			
			if (m_registeredReaders.ContainsKey(fileExtension) == false)
			{
				string msg = string.Format("Extension ({0}) not registered!", fileExtension);
				throw new ArgumentException(msg);
			}
			
			return m_registeredReaders[fileExtension];
		}
		
		private Dictionary<string, IRaidFileReader> m_registeredReaders = new Dictionary<string, IRaidFileReader>();
	}
}
